package com.useeinfo.oa.modules.maintaining.biz;

import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.maintaining.dao.MaintainOrderLogDao;
import com.useeinfo.oa.modules.maintaining.entity.MaintainOrder;
import com.useeinfo.oa.modules.maintaining.entity.MaintainOrderLog;
import com.useeinfo.oa.modules.maintaining.enums.OrderOperationEnum;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Auther: xudong.sun
 * @Date: 2019/1/3 10:33
 * @Description:
 */
@Service
public class MaintainOrderLogBiz extends CrudBiz<MaintainOrderLogDao, MaintainOrderLog> {
    public void saveMaintainOrderLog(MaintainOrder maintainOrder, OrderOperationEnum orderOperationEnum, HttpServletRequest request){
        SystemUser systemUser= SessionKeyUtils.getSessionUser(request);
        MaintainOrderLog maintainOrderLog=new MaintainOrderLog();
        maintainOrderLog.setMaintainOrder(maintainOrder);
        maintainOrderLog.setEnterprise(SessionKeyUtils.getSessionEnterprise(request));
        maintainOrderLog.setSystemUser(systemUser);
        maintainOrderLog.setUserName(systemUser.getUserName());
        String detail=systemUser.getUserName()+"在"+ DateTimeUtils.formatDateToStringWithTime(new Date())+","
                +orderOperationEnum.getName()+"，操作工单名称为："+maintainOrder.getOrderName()+",编号为："+maintainOrder.getId();
        maintainOrderLog.setDetail(detail);
        maintainOrderLog.setOrderOperationEnum(orderOperationEnum);
        dao.add(maintainOrderLog);
    }

    public void saveMaintainOrderLogByCache(MaintainOrder maintainOrder, OrderOperationEnum orderOperationEnum, SystemUser systemUser, Enterprise enterprise, Date createDate){
        MaintainOrderLog maintainOrderLog = new MaintainOrderLog();
        maintainOrderLog.setMaintainOrder(maintainOrder);
        maintainOrderLog.setEnterprise(enterprise);
        maintainOrderLog.setSystemUser(systemUser);
        maintainOrderLog.setUserName(systemUser.getUserName());
        String detail=systemUser.getUserName()+"在"+ DateTimeUtils.formatDateToStringWithTime(new Date())+","
                +orderOperationEnum.getName()+"，操作工单名称为："+maintainOrder.getOrderName()+",编号为："+maintainOrder.getId();
        maintainOrderLog.setDetail(detail);
        maintainOrderLog.setOrderOperationEnum(orderOperationEnum);
        maintainOrderLog.setCreateDate(createDate);
        dao.add(maintainOrderLog);
    }
}
